<?php
//sleep(5);
     
    include_once('connection/connection.php');
    if(isset($_REQUEST['load_division'])) {
	//sleep(4);
		$query = mysql_query("SELECT div_id , div_name FROM tbl_lookup_division WHERE circle_id='$_REQUEST[load_division]' AND status = '1' ");
		echo "<option value='0'>select division: </option>";
		while($circle = mysql_fetch_assoc($query)){
		echo "<option value='".$circle['div_id']."'>".$circle['div_name']."</option>";
		}
		unset($query);
	
	}
	
	if(isset($_REQUEST['obj_add'])){
		$add = json_decode($_REQUEST['obj_add'],true);
		
		mysql_query("INSERT INTO tbl_lookup_sub_division VALUES('','$add[sub_div_name]','$add[div_id]',(SELECT div_name FROM tbl_lookup_division WHERE div_id=$add[div_id]),'$add[circle_id]',(SELECT circle_name FROM tbl_lookup_circle WHERE circle_id=$add[circle_id]),'$add[zone_id]',(SELECT zone_name FROM tbl_lookup_zone WHERE zone_id=$add[zone_id]),'1')");
		$last_id = mysql_insert_id();
		$query = mysql_query("SELECT * FROM tbl_lookup_sub_division WHERE sub_div_id='$last_id'");
		$div = mysql_fetch_assoc($query);
		echo "<tr onMouseOver=\" this.style.backgroundColor='#E5E5E5'; \" onMouseOut= \"this.style.backgroundColor='#FFFFFF'; \" id= 'row_id_".$div['sub_div_id']."' class='sub_div_row'>";
		echo "<td><font style='font-size: 8.5pt' face='Verdana' color='#000000'>".$div['zone_name']."</font></td>";
		echo "<td><font style='font-size: 8.5pt' face='Verdana' color='#000000'>".$div['circle_name']."</font></td>";
		echo "<td><font style='font-size: 8.5pt' face='Verdana' color='#000000'>".$div['div_name']."</font></td>";
		
		echo "<td height='27' align='center' id='div_id_".$div['sub_div_id']."'  ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>".$div['sub_div_name']."</font></td>";
		echo "<td align='center'><a href='Javascript:void(0);' onClick= \"edit_div('".$div['sub_div_id']."'); \" ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Edit</font></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		echo "<span id='state_id_".$div['sub_div_id']."' ><a href='Javascript:void(0);' onClick= \" status_handler(0,'".$div['sub_div_id']."'); \"  ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Disable</font></a></span></td></tr>";

	unset($last_id);
	unset($query);
	unset($div);
	unset($add);

		
	
	}
	
	if(isset($_REQUEST['status'])) {

			if(mysql_query("UPDATE tbl_lookup_sub_division SET status = '$_REQUEST[status]' WHERE sub_div_id='$_REQUEST[id]' ")) {
					if($_REQUEST['status']==1) echo "<a href='Javascript:void(0);' onClick=\"status_handler(0,'".$_REQUEST['id']."'); \" ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Disable</font></a>";
					else echo "<a href='Javascript:void(0);' onClick=\"status_handler(1,'".$_REQUEST['id']."'); \" ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Enable</font></a>";
			}
			else{
			echo mysql_error();
			}
	}
	
	if(isset($_REQUEST['edit_id'])){
	//print_r($_REQUEST);
	
	$query = mysql_query("SELECT * FROM tbl_lookup_sub_division WHERE sub_div_id = '$_REQUEST[edit_id]' ");
	$detail = mysql_fetch_assoc($query);
	
	echo "<td><select id='zone_id' onChange= \" load_circle(this.value, 'circle_id'); \" >";
	$query = mysql_query("SELECT * FROM tbl_lookup_zone WHERE status = 1  ORDER BY zone_id desc");
	echo "<option value='0'>select zone:</option>";
		while($zone = mysql_fetch_assoc($query)){
			if($zone['zone_id'] == $detail['zone_id']) {
			echo "<option value='".$zone['zone_id']."' selected>".$zone['zone_name']."</option>";
			}
			else{
			echo "<option value='".$zone['zone_id']."'>".$zone['zone_name']."</option>";
			}
		}
	echo "</select></td>";
	
	echo "<td><select id='circle_id' onChange = \" load_division(this.value,'division_id'); \" >";
	$query = mysql_query("SELECT * FROM tbl_lookup_circle WHERE zone_id= '$detail[zone_id]' AND status = 1 ORDER BY circle_id desc");
		echo "<option value='0'>select circle:</option>";
		while($circle = mysql_fetch_assoc($query)){
			if($circle['circle_id'] == $detail['circle_id']) {
			echo "<option value='".$circle['circle_id']."' selected>".$circle['circle_name']."</option>";
			}
			else{
			echo "<option value='".$circle['circle_id']."'>".$circle['circle_name']."</option>";
			}
		}
	echo "</select></td>";
	
		echo "<td><select id='division_id'  >";
	$query = mysql_query("SELECT * FROM tbl_lookup_division WHERE circle_id= '$detail[circle_id]' AND status = 1 ORDER BY div_id desc");
		echo "<option value='0'>select division:</option>";
		while($circle = mysql_fetch_assoc($query)){
			if($circle['div_id'] == $detail['div_id']) {
			echo "<option value='".$circle['div_id']."' selected>".$circle['div_name']."</option>";
			}
			else{
			echo "<option value='".$circle['div_id']."'>".$circle['div_name']."</option>";
			}
		}
	echo "</select></td>";

	
	
	echo "<td align='center'><input type='text' id='sub_name' value='".$detail['sub_div_name']."' size='15'  /></td>";
	echo "<td align='center'><a href='Javascript:void(0);' onClick= \" save_changes('".$_REQUEST['edit_id']."','sub_name','division_id','circle_id','zone_id');  \" ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>save</font> </a>&nbsp;&nbsp;&nbsp;<a href='Javascript:void(0);' onClick= \" default_row('".$_REQUEST['edit_id']."');  \" ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Cancel</font> </a></td>";
	
	}
	
	if(isset($_REQUEST['change_subdiv'])){
		$data = json_decode($_REQUEST['change_subdiv'],true);
		//print_r($data);
		
		
		
		if(mysql_query("UPDATE tbl_lookup_sub_division SET sub_div_name = '$data[sub_div_name]' ,div_id = '$data[div_id]',div_name = (SELECT div_name FROM tbl_lookup_division WHERE div_id= $data[div_id]) , circle_id='$data[circle_id]' , circle_name = (SELECT circle_name FROM tbl_lookup_circle WHERE circle_id= $data[circle_id]) , zone_id = '$data[zone_id]' , zone_name = (SELECT zone_name FROM tbl_lookup_zone WHERE zone_id= $data[zone_id]) WHERE sub_div_id = '$data[sub_div_id]'")){
		$query = mysql_query("SELECT * FROM tbl_lookup_sub_division WHERE sub_div_id='$data[sub_div_id]' ");
		$detail = mysql_fetch_assoc($query);
		echo "<td><font style='font-size: 8.5pt' face='Verdana' color='#000000'>".$detail['zone_name']."</font></td>";
		echo "<td><font style='font-size: 8.5pt' face='Verdana' color='#000000'>".$detail['circle_name']."</font></td>";
		echo "<td><font style='font-size: 8.5pt' face='Verdana' color='#000000'>".$detail['div_name']."</font></td>";
		echo "<td height='27' id='div_id_". $detail['sub_div_id'] ."' align='center'><font style='font-size: 8.5pt' face='Verdana' color='#000000'>".$detail['sub_div_name']."</font></td>";
		echo "<td align='center'><a href='Javascript:void(0);' onClick=\" edit_div('".$detail['sub_div_id']."'); \" ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Edit</font></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		echo "<span id='state_id_".$detail['sub_div_id']."'>";
				if($detail['status'] == "1") echo "<a href='Javascript:void(0);' onClick=\"status_handler(0,'".$detail['sub_div_id']."'); \" ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Disable</font></a>";
				else echo "<a href='Javascript:void(0);' onClick=\"status_handler(1,'".$detail['sub_div_id']."'); \" ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Enable</font></a>";
		
		echo "</span></td>";
		echo "";
		unset($query);
		unset($detail);
		}
		else{
		echo mysql_error();
		}
		
	}

	if(isset($_REQUEST['search'])) {
	
	$query = mysql_query("SELECT DISTINCT sub_div_id FROM tbl_lookup_sub_division WHERE sub_div_name like '$_REQUEST[search]%' OR div_name like '$_REQUEST[search]%' OR circle_name like '$_REQUEST[search]%' OR zone_name like '$_REQUEST[search]%' ");
		$sub_id = array();
		while($data = mysql_fetch_assoc($query)) {
			$sub_id[] = $data;
		}
		
		echo json_encode($sub_id);
		
	}
	
	if(isset($_REQUEST['load'])) {
	//echo $_REQUEST['load'];
	//echo $_REQUEST['start'];
	//sleep(1);
	$query="";
	$totol="";
	
	$current_page =($_REQUEST['start']-1)*10;
	
	if($_REQUEST['load']==""){
	$query = mysql_query("SELECT sd.sub_div_id FROM tbl_lookup_sub_division sd , tbl_lookup_division d , tbl_lookup_circle c , tbl_lookup_zone z WHERE sd.div_id = d.div_id AND sd.circle_id = c.circle_id AND sd.zone_id = z.zone_id AND d.status = 1 AND c.status =1 AND z.status = 1");
	$total = mysql_num_rows($query);
	$query = mysql_query("SELECT sd.* FROM tbl_lookup_sub_division sd , tbl_lookup_division d , tbl_lookup_circle c , tbl_lookup_zone z WHERE sd.div_id = d.div_id AND sd.circle_id = c.circle_id AND sd.zone_id = z.zone_id AND d.status = 1 AND c.status =1 AND z.status = 1 LIMIT $current_page , 10");
	}
	else{
	$query = mysql_query("SELECT DISTINCT sd.sub_div_id FROM tbl_lookup_sub_division sd , tbl_lookup_division d , tbl_lookup_circle c , tbl_lookup_zone z WHERE sd.div_id = d.div_id AND sd.circle_id = c.circle_id AND sd.zone_id = z.zone_id AND d.status = 1 AND c.status =1 AND z.status = 1 AND (sd.sub_div_name like '$_REQUEST[load]%' OR sd.div_name like '$_REQUEST[load]%' OR sd.circle_name like '$_REQUEST[load]%' OR sd.zone_name like '$_REQUEST[load]%' ) ");
	$total = mysql_num_rows($query);
	$query = mysql_query("SELECT DISTINCT sd.* FROM tbl_lookup_sub_division sd , tbl_lookup_division d , tbl_lookup_circle c , tbl_lookup_zone z WHERE sd.div_id = d.div_id AND sd.circle_id = c.circle_id AND sd.zone_id = z.zone_id AND d.status = 1 AND c.status =1 AND z.status = 1 AND (sd.sub_div_name like '$_REQUEST[load]%' OR sd.div_name like '$_REQUEST[load]%' OR sd.circle_name like '$_REQUEST[load]%' OR sd.zone_name like '$_REQUEST[load]%' )   LIMIT $current_page, 10");
	}
	//echo mysql_affected_rows();
	$total_pages = ceil($total/10);
	
		if(mysql_num_rows($query)==0) {
		echo "<tr height='50'><td colspan='4' align='center'><font style='font-size: 10pt' face='Verdana' color='red'>No Record Found</font></td></tr>";
		}
		
		while($data = mysql_fetch_assoc($query)) {
		echo "<tr onMouseOver=\" this.style.backgroundColor='#E5E5E5'; \" onMouseOut= \"this.style.backgroundColor='#FFFFFF'; \" id= 'row_id_".$data['sub_div_id']."' class='sub_div_row'>";
		echo "<td><font style='font-size: 8.5pt' face='Verdana' color='#000000'>".$data['zone_name']."</font></td>";
		echo "<td><font style='font-size: 8.5pt' face='Verdana' color='#000000'>".$data['circle_name']."</font></td>";
		echo "<td><font style='font-size: 8.5pt' face='Verdana' color='#000000'>".$data['div_name']."</font></td>";
	
		echo "<td align='center' height='27'  id='div_id_".$data['sub_div_id']."'  ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>".$data['sub_div_name']."</font></td>";
			
		echo "<td align='center'  ><a href='Javascript:void(0);' onClick= \"edit_div('".$data['sub_div_id']."'); \" ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Edit</font></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		echo "<span id='state_id_".$data['sub_div_id']."' >";
		
		if($data['status']==1)	echo "<a href='Javascript:void(0);' onClick= \" status_handler(0,'".$data['sub_div_id']."'); \"  ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Disable</font></a>";
		else echo "<a href='Javascript:void(0);' onClick= \" status_handler(1,'".$data['sub_div_id']."'); \"  ><font style='font-size: 8.5pt' face='Verdana' color='#000000'>Enable</font></a>";
		echo "</span></td></tr>";
		
		}
		echo "<tr class='sub_div_row' bgcolor='#E5E5E5' id='page_link'><td colspan ='5' align='center'>";
			if($_REQUEST['start']!=1) echo "<a href='javascript:void(0);' onClick= \"ajax_content('".$_REQUEST['load']."','1'); \" ><b>First</b></a>";
		for($i=1;$i<=$total_pages;$i++){
		echo "&nbsp;&nbsp;";
			
		echo "&nbsp;&nbsp;";
			if($i==$_REQUEST['start']) echo "<big>".$i."</big>";
			else echo "<a href='javascript:void(0);' onClick= \"ajax_content('".$_REQUEST['load']."','".$i."'); \" ><b>".$i."</b></a>";
		echo "&nbsp;&nbsp;";
			
		}
			if($_REQUEST['start'] < $total_pages) echo "<a href='javascript:void(0);' onClick= \"ajax_content('".$_REQUEST['load']."','".$total_pages."'); \" ><b>Last</b></a>";
		echo "</td></tr>";
		
	
	}
	
	
	
	
	

?>